package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.About;
import com.hngm.bean.Product;
import com.hngm.bean.Productpicture;
import com.hngm.bean.Producttypetable;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.ProductDao;
import com.hngm.dao.ProductpictureDao;
import com.hngm.dao.ProducttypetableDao;

import jdk.nashorn.api.scripting.JSObject;
import jdk.nashorn.internal.runtime.JSONFunctions;

/**
 * Servlet implementation class ProductlistServlet
 */
public class ProductlistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductlistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		
		//获取请求中的参数url
		String url = request.getParameter("url");
		//创建ProductDao
		ProductDao productDao = new ProductDao();
		ProducttypetableDao producttypetableDao = new ProducttypetableDao();
		//查询图片列表
		ProductpictureDao productpictureDao = new ProductpictureDao();
		
		if("proList".equals(url)){
			//查询产品列表和产品类型
			//获取前端页面传递过来的产品类型
			String pType = request.getParameter("pType");
			
			//根据上面获取的产品类型去查询对应的产品信息
			List<Product> pList = productDao.queryProductListByType(pType);
			request.setAttribute("pList", pList);
			
			//查询产品类型
			ArrayList<Producttypetable> prList = producttypetableDao.queryProducttypetableList();
			request.setAttribute("prList", prList);
			//去记录当前用户点击的是哪一种类型
			for(int i = 0 ; i < prList.size() ; i++){
				if(pType.equals(prList.get(i).getId()+"")){
					//将当前用户选中的类型,存储到request中
					request.setAttribute("curType", prList.get(i));
				}
			}
			
			request.getRequestDispatcher("productlist.jsp").forward(request, response);
		}else if("add".equals(url)){
			//添加产品逻辑
			//1、获取前端传过来的信息
			String productname = request.getParameter("productname");
			Producttypetable p = new Producttypetable();
			p.setProductname(productname);
			//2、产品类型信息插入到表中（调用DAO）
			int result = producttypetableDao.insertProduttype(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		}else if("del".equals(url)){
			//删除产品逻辑
			//删除产品逻辑
			//1、获取前端传过来的产品Id
			String productrypeId = request.getParameter("productrypeId");
			//2、根据产品Id删除产品信息
			int result = producttypetableDao.delProducttype(productrypeId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		}else if("proDetail".equals(url)){
			
			
			//根据产品ID查询产品详情
			String pid = request.getParameter("pid");
			//查询数据库
			Product p = productDao.queryProductInfoById(pid);
			request.setAttribute("productInfo", p);
			
			String pType = p.getProducttypetable_id()+"";
			
			//根据上面获取的产品类型去查询对应的产品信息
			List<Product> pList = productDao.queryProductListByType(pType);
			request.setAttribute("pList", pList);
			//查询产品类型
			ArrayList<Producttypetable> prList = producttypetableDao.queryProducttypetableList();
			request.setAttribute("prList", prList);
			//存储当前当前产品类型
			//去记录当前用户点击的是哪一种类型
			for(int i = 0 ; i < prList.size() ; i++){
				if(pType.equals(prList.get(i).getId()+"")){
					//将当前用户选中的类型,存储到request中
					request.setAttribute("curType", prList.get(i));
				}
			}
						
			//转发到产品详情页面
			request.getRequestDispatcher("productdetails.jsp").forward(request, response);
		}else if("queryProductListBypType".equals(url)){
			//获取前端传过来的查询条件
			String producttypename = request.getParameter("producttypename");
			String del = request.getParameter("del");
			//获取LayUI穿过来的分页信息
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = Integer.parseInt(request.getParameter("page"));
			
			//当前页开始的索引
			int beginIndex = (page - 1) * limit;
			
			//根据传过来得查询条件去查询产品列表
			ArrayList<Producttypetable> pList = producttypetableDao.queryProductListBypType(producttypename,del,beginIndex,limit);
			
			//查询数据库中的数量
			int count = producttypetableDao.queryProductListBypTypeCount(producttypename,del);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, pList);
			out.print(str);
		}else if("typeLsit".equals(url)){
			//查询所有的产品类型列表
			ArrayList<Producttypetable> tList = producttypetableDao.queryProducttypetableList();
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品类型列表转换成JSON格式的字符串
			String str = ResultList.toClient(0, "成功", 0, tList);
			out.print(str);
		}else if("edit".equals(url)){
			//编辑产品逻辑
			//1、获取前端传过来的信息
			long id = Long.parseLong(request.getParameter("id"));
			String productname = request.getParameter("productname");
			Producttypetable p = new Producttypetable();
			p.setProductname(productname);
			p.setId(id);
			//2、产品类型信息插入到表中（调用DAO）
			int result = producttypetableDao.editProduttype(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "编辑成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "编辑失败"));
			}
		
		}
		out.flush();
		out.close();
			
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
